import com.aliyun.oss.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;

public class push {
    public static void main(String[] args) throws Exception {
        // 要上传的文件路径
        String filePath_o = "C:/Users/李进峰/Downloads/顾客默认头像.png";







        String fileName_o = filePath_o.split("/")[filePath_o.split("/").length - 1];
        // 填写您的AccessKey ID和AccessKey Secret
        String accessKeyId = "LTAI5tKFf9cjiA8hzS8Sq8Cu";
        String accessKeySecret = "DFIaG9jTtuKrM9pvda7T0TBT2NQrBt";
        // 填写Bucket名称
        String bucketName = "rsl-plus";
        // 填写Object完整路径
        String objectName = fileName_o;
        // 填写本地文件的完整路径
        String filePath = filePath_o;
        // 无地域的Endpoint
        String endpoint = "oss-rg-china-mainland.aliyuncs.com";

        // 创建OSSClient实例
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 使用V2签名
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V2);
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, clientBuilderConfiguration);

        try {
            // 创建PutObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new File(filePath));
            // 上传文件
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            System.out.println("文件上传成功！");
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}